class Utils {
	public constructor() {
	}
	/**
	 * 从两个整数中随机一个数出来，minNum:随机出来的最小数，maxNum:随机出来的最大数
	 * @param minNum:随机出来的最小数
	 * @param maxNum:随机出来的最大数
	 */
	public static randomIntNum(minNum:number,maxNum:number):number{
		return Math.floor(Math.random()*(maxNum-minNum+1))+minNum;
	}
	public static randomFromArray(list:Array<any>):any
	{
		if(list && list.length){
			let index = Utils.randomIntNum(0,list.length-1);
			return list[index];
		}else{
			return null;
		}
	}
	public static randomFromArrayWithPersent(list:Array<any>):any{
		
		let random = Math.random();
		if(list && list.length){
			
			for(let i=0;i<list.length;i++){
				let data = list[i];
				if(data.fromRate <=random && random<data.toRate){
					return data;
				}
			}
		}else{
			return null;
		}
	}

	public static saveItem2Storage(key:string,value:string): void{
		egret.localStorage.setItem(GameConfig.gameName+"_"+key, value);
	}
	public static getItemFromStorage(key:string): any{
		return egret.localStorage.getItem(GameConfig.gameName+"_"+key);
	}
	/**
	 * 分享信息并判断
	 * 1、分享的是否是群
	 * 2、分享的群ticket去查询getShareInfo获得iv和encryptedData
	 * 3、通过iv/encryptedData查询接口分享的是否是新群
	 * 4、回调
	 */
	public static shareAppMessageWithCheckGroup(shareData:any,callback:((shareSuccess:boolean,shareTicket:string,iv:string,encryptedData:string)=>void), thisObj: any){
		
		// 绑定通过shareTicket获取iv、encryptedData
		if(thisObj){
			thisObj._utils_getShareInfo = function(shareTicket:string){
				platform.callPlatformMethod("getShareInfo",
									{shareTicket:shareTicket,
									success:function(res){
										if(callback){
											callback.call(thisObj,true,shareTicket,res.iv,res.encryptedData);
										}
									},
									fail:function(err){
										if(callback){
											callback.call(thisObj,true,shareTicket,null,null);
										}
									}});
			}
		}
		// 绑定分享success回调
		if(thisObj && shareData && !shareData.success){
			let shareSuccessCall = function(res){
				let shareTickets = res.shareTickets;
				if(shareTickets && shareTickets[0]){
					// 分享成功到群且能拿到分享的shareTickets
					thisObj._utils_getShareInfo(shareTickets[0]);
				}else{
					// 分享成功但分享的不是群
					if(callback){
						callback.call(thisObj,true,null,null,null);
					}
					
				}
			}
			shareData.success = shareSuccessCall.bind(thisObj);
		}
		// 绑定分享success回调
		if(thisObj && shareData && !shareData.fail){
			let shareFailCall = function(err){
				// 分享失败
				if(callback){
					callback.call(thisObj,false,null,null,null);
				}
			}
			shareData.fail = shareFailCall.bind(thisObj);
		}
		platform.shareAppMessage(shareData);
		
	}
	public static callWXSuccess(res:any): boolean{
		if(res && res.errMsg && res.errMsg.length>3){
			let msg = res.errMsg as string;
			let lastMsg = msg.substr(msg.length-3,3);
			
			if(lastMsg == ":ok"){
				return true;
			}
		}
		return false;
	}
}